﻿Imports Training.Common

Partial Public Class DisableGroup
    Inherits System.Web.UI.Page
    Public gName As String ' group name
    Dim gid As Integer

    ''' <summary>
    ''' Page Load event, check for querystring data and assign to variables
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        ' check for querystring data
        If IsNothing(Request.QueryString("gid")) Or IsNothing(Request.QueryString("nm")) Then
            Response.Redirect("Groups.aspx")
        Else
            gid = Request.QueryString("gid")
            gName = Request.QueryString("nm")

            ' set label
            SetLabel(gid)
        End If
    End Sub

    ''' <summary>
    ''' Set label with access status of group login account
    ''' </summary>
    ''' <param name="gid"></param>
    ''' <remarks>toggle button visibity based on lockout status</remarks>
    Private Sub SetLabel(ByVal gid)
        ' get loginname
        Dim lName As String
        lName = GetGroupLogin(gid)

        Dim user As MembershipUser = Membership.GetUser(lName)
        If user.IsApproved Then
            lblCurrent.Text = "Group: <b>" & gName & "</b> User: <b>" & lName & "</b> currently has login access."
            btnDisable.Visible = True
            btnEnable.Visible = False
        Else
            lblCurrent.Text = "Group: <b>" & gName & "</b> User: <b>" & lName & "</b> is currently locked out."
            btnDisable.Visible = False
            btnEnable.Visible = True
        End If
    End Sub

    ''' <summary>
    ''' Disable button click event. changes users approved status to false
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks>this will NOT affect Users who are part of the group</remarks>
    Private Sub btnDisable_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDisable.Click
        ' get loginname
        Dim lName As String
        lName = GetGroupLogin(gid)

        Dim user As MembershipUser = Membership.GetUser(lName)
        user.IsApproved = False
        Membership.UpdateUser(user)

        ' redirect back to Group Management
        Response.Redirect("Groups.aspx")
    End Sub

    ''' <summary>
    ''' Enable button click event. changes users approved status to true
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnEnable_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnEnable.Click
        ' get loginname
        Dim lName As String
        lName = GetGroupLogin(gid)

        Dim user As MembershipUser = Membership.GetUser(lName)
        user.IsApproved = True
        Membership.UpdateUser(user)

        ' redirect back to Group Management
        Response.Redirect("Groups.aspx")
    End Sub
End Class